package com.alog.yzd.bookmall.dao.user;

import com.alog.yzd.bookmall.domain.user.User;
import com.alog.yzd.bookmall.utils.jdbc.TxQueryRunner;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

/**
 * 用户模块的持久层
 */
public class UserDao {
    private QueryRunner runner = new TxQueryRunner();

    /**
     * 根据Uid和password查找用户
     */
    public boolean findByUidAndPassword(String uid, String password) throws SQLException {
        String sql = "select count(*) from t_user where uid = ? and loginpass = ?";
        Object[] params = new Object[]{uid, password};
        Number number = runner.query(sql, new ScalarHandler<Number>(), params);
        if(number.intValue() == 1){
            return true;
        }
        return false ;
    }

    /**
     * 更新用户密码
     * @param uid
     * @param password
     * @throws SQLException
     */
    public void updatePassword(String uid , String password) throws SQLException {
        String sql = "update t_user set loginpass = ? where uid = ?";
        Object[] params = new Object[]{password, uid};
        runner.update(sql, params);
    }

    /**
     * 通过用户名和密码查找用户
     * @param loginname
     * @param loginpass
     * @return
     * @throws SQLException
     */
    public User findByLoginnameAndLoginpass(String loginname, String loginpass) throws SQLException {
        String sql = "select * from t_user where loginname = ? and loginpass = ?";
        Object[] params = new Object[]{loginname, loginpass};
        User user = runner.query(sql, new BeanHandler<User>(User.class), params);
        return user;
    }
    /**
     * 通过激活码查找用户
     * @param code
     * @return
     * @throws SQLException
     */
    public User findByActivationCode(String code) throws SQLException {
        String sql = "select * from t_user where activationCode = ?";
        Object[] params = new Object[]{code};
        User user = runner.query(sql, new BeanHandler<User>(User.class), params);
        return user;
    }

    /**
     * 修改用户状态
     * @param uid
     * @param status
     */
    public void updateStatus(String uid, boolean status) throws SQLException {
        String sql = "update t_user set status = ? where uid = ?";
        Object[] params = new Object[]{status, uid};
        runner.update(sql, params);
    }
    /**
     * 添加用户
     * @param user
     * @throws SQLException
     */
    public void addUser(User user) throws SQLException {
        String sql = "insert into t_user values(?, ?, ?, ?, ?, ?)";
        Object[] params = {user.getUid(), user.getLoginname(), user.getLoginpass(), user.getEmail(), user.isStatus(), user.getActivationCode()};
        runner.update(sql, params);
    }
    /**
     * 校验用户名是否已经存在
     * @param loginname
     * @return
     * @throws SQLException
     */
    public boolean ajaxValidateLoginname(String loginname) throws SQLException {
        String sql = "select count(1) from t_user where loginname = ?";
        Number number = runner.query(sql, new ScalarHandler<Number>(), loginname);
        return number.intValue() == 0 ? true : false;
    }

    /**
     * 校验邮箱是否已经存在
     * @param email
     * @return
     * @throws SQLException
     */
    public boolean ajaxValidateEmail(String email) throws SQLException {
        String sql = "select count(1) from t_user where email = ?";
        Number number = runner.query(sql, new ScalarHandler<Number>(), email);
        return number.intValue() == 0 ? true : false;
    }




}
